import 'package:flutter/foundation.dart';
import 'package:xws/helpers/safe_change_notifier.dart';
import 'package:xws/models/goods/goods_obj.dart';
import 'package:xws/widgets/refresh_widget/refresh_load_view.dart';

class _AState {
  _AState._();
  static final AppState _userState = AppState();
}

AppState get apps => _AState._userState;

/// app状态管理
class AppState extends ChangeNotifier with SafeChangeNotifier {
  AppState() {
    unRead = 0;
  }

  RefreshViewController<GoodsObj>? nearGoodsController;

  RefreshViewController<GoodsObj>? friendsGoodsController;

  late int unRead;

  /// 刷新附近好物
  Future<void> refreshNearGoods() async {
    await nearGoodsController?.refresh();
    notifyListeners();
  }

  /// 刷新好友发布
  Future<void> refreshFriendGoods() async {
    await friendsGoodsController?.refresh();
    notifyListeners();
  }

  /// * 设置未读消息数
  void setUnread(int count) {
    if (count == unRead) return;

    unRead = count;
    notifyListeners();
  }

  /// * 增加未读消息数
  /// * 可以为负数
  void changeUnread(int count) {
    if (count == 0) return;

    unRead += count;
    notifyListeners();
  }
}
